﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.Sys.Orders.BuyFinish
{
    public partial class Form : FormPage
    {
        BLL.Orders bll = new BLL.Orders();
        Model.Orders model = new Model.Orders();
        //
        BLL.Orders_Goods bllOrders_Goods = new BLL.Orders_Goods();
        BLL.Goods bllGoods = new BLL.Goods();
        //
        BLL.Store bllStore = new BLL.Store();
        Model.Store modelStore = new Model.Store();
        //
        BLL.Account_Change_Log bllAccount_Change_Log = new BLL.Account_Change_Log();
        BLL.Orders_State_Log bllOrders_State_Log = new BLL.Orders_State_Log();
        //
        BLL.Express bllExpress = new BLL.Express();
        protected void Page_Load(object sender, EventArgs e) { }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel() { model = bll.GetModel(Master.id); modelStore = bllStore.GetModel(model.StoreID); }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {

        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            int ExpressID = DataTypePlus.requestInt(DDL_Express.SelectedValue);
            decimal ExpressPrice = DataTypePlus.requestDecimal(tb_ExpressPrice.Text);
            string ExpressNumber = DataTypePlus.requestString(tb_ExpressNumber.Text);


            //1.支付货款
            CallBak<int> callBak_BuySuccess = BLL.Account_Change_Log.Add(EnumHelper.UserType.Store, model.StoreID, EnumHelper.AccountChangeReason.Success_BuyOrders, Master.id, model.ActualPriceSum);//扣孕婴店
            if (!callBak_BuySuccess.IsSuccess) { Master.ScriptManager_Alert_float(callBak_BuySuccess.ErrMsg, false); return; }

            //2.扣运费
            CallBak<int> callBak_PayExpressMoney= BLL.Account_Change_Log.Add(EnumHelper.UserType.Store, modelStore.id, EnumHelper.AccountChangeReason.Pay_ExpressMoney, Master.id, ExpressPrice);
            if (!callBak_PayExpressMoney.IsSuccess) { Master.ScriptManager_Alert_float(callBak_PayExpressMoney.ErrMsg, false); return; }


            model.ExpressID = ExpressID;
            model.ExpressPrice = ExpressPrice;
            model.ExpressNumber = ExpressNumber;
            model.State = (int)EnumHelper.OrdersState.HasSend;
            model.DateandtimeSend = DateTime.Now;
            model.IsBuyMoneyOK = model.ActualPay + model.ExpressPrice == model.ExpectPay + 100;
            bll.Update(model);
            BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Orders, Master.id, EnumHelper.OperateType.Update, "");
            //订单状态日志
            Model.Orders_State_Log modelOrders_State_Log = new Model.Orders_State_Log();
            modelOrders_State_Log.OrdersID = Master.id;
            modelOrders_State_Log.State = model.State;
            int Orders_State_LogID_new = bllOrders_State_Log.Add(modelOrders_State_Log);
            BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Orders_State_Log, Orders_State_LogID_new, EnumHelper.OperateType.Add,"");
            //扣除运费后，孕婴店金额发生变化，所以要重新获取modelStore
            modelStore = bllStore.GetModel(model.StoreID);
            //通知孕婴店
            BLL.Message bllMessage = new BLL.Message();
            bllMessage.NewMessage(0, Security.GetCurrentUserID(),modelStore.UserID,
                "您本周采购的商品已采购完毕，共计金额" + StringPlus.showMoney(model.ActualPriceSum) + "元，已通过" + DDL_Express.SelectedExpressName + "物流发出，运单号" + model.ExpressNumber + "，联系电话" + bllExpress .GetModel(model.ExpressID).Mobile+ "，运费" + model.ExpressPrice + "元，您帐户剩余金额为"+modelStore.Account+"元",
                //"您的订单已经发货，编号："+model.Number+"，金额："+StringPlus.showMoney(model.ActualPriceSum)+"，使用物流："+DDL_Express.SelectedExpressName+"，发货编号："+model.ExpressNumber+"",
                true,true);
            


            //if (model.ActualPriceSum > 0)
            //{
            //    //账户变动:采购
            //    Model.Account_Change_Log modelMoney_Change_Log = new Model.Account_Change_Log();
            //    modelMoney_Change_Log.StoreID = model.StoreID;
            //    modelMoney_Change_Log.AccountBefore = modelStore.Account;
            //    modelMoney_Change_Log.Account = model.ActualPriceSum;
            //    modelMoney_Change_Log.AccountAfter = modelMoney_Change_Log.AccountBefore - modelMoney_Change_Log.Account;
            //    modelMoney_Change_Log.Reason = (int)EnumHelper.AccountChangeReason.Buy;
            //    modelMoney_Change_Log.ReasonRowID = Master.id;
            //    modelMoney_Change_Log.Type = (int)EnumHelper.AccountChangeType.Decrease;
            //    int Money_Change_LogID_new = bllMoney_Change_Log.Add(modelMoney_Change_Log);
            //    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Money_Change_Log, Money_Change_LogID_new, EnumHelper.OperateType.Add, "扣除订单"+model.Number+"采购款"+StringPlus.showMoney(model.ExpectPriceSum));
            //    //扣除余额：采购
            //    modelStore.Account -= modelMoney_Change_Log.Account;
            //    bllStore.Update(modelStore);
            //    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Store, model.StoreID, EnumHelper.OperateType.Update, "扣除订单" + model.Number + "采购款" + StringPlus.showMoney(model.ExpectPriceSum));
            //}
            //if (model.ExpressPrice > 0)
            //{
            //    //账户变动:采购
            //    Model.Account_Change_Log modelMoney_Change_Log = new Model.Account_Change_Log();
            //    modelMoney_Change_Log.StoreID = model.StoreID;
            //    modelMoney_Change_Log.AccountBefore = modelStore.Account;
            //    modelMoney_Change_Log.Account =  model.ExpressPrice;
            //    modelMoney_Change_Log.AccountAfter = modelMoney_Change_Log.AccountBefore - modelMoney_Change_Log.Account;
            //    modelMoney_Change_Log.Reason = (int)EnumHelper.AccountChangeReason.Express;
            //    modelMoney_Change_Log.ReasonRowID = Master.id;
            //    modelMoney_Change_Log.Type = (int)EnumHelper.AccountChangeType.Decrease;
            //    int Money_Change_LogID_new = bllMoney_Change_Log.Add(modelMoney_Change_Log);
            //    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Money_Change_Log, Money_Change_LogID_new, EnumHelper.OperateType.Add, "扣除订单" + model.Number + "运费" + StringPlus.showMoney(model.ExpressPrice));
            //    //扣除余额：采购
            //    modelStore.Account -= modelMoney_Change_Log.Account;
            //    bllStore.Update(modelStore);
            //    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Store, model.StoreID, EnumHelper.OperateType.Update, "扣除订单" + model.Number + "运费" + StringPlus.showMoney(model.ExpressPrice));
            //}
            //decimal Money = DataTypePlus.requestDecimal(tb_Money.Text);
            //if (rbl_MoneyType.SelectedValue == "1" && Money == 0) { Master.ScriptManager_Alert_float("【货款金额】必须填写", false); return; }
            //if (rbl_MoneyType.SelectedValue == "1")
            //{//收款
            //    Model.Money_Change_Log modelMoney_Change_Log = new Model.Money_Change_Log();
            //    modelMoney_Change_Log.StoreID = model.StoreID;
            //    modelMoney_Change_Log.Money = Money;
            //    modelMoney_Change_Log.Type = (int)EnumHelper.MoneyChangeType.Increase;
            //    modelMoney_Change_Log.Reason = (int)EnumHelper.MoneyChangeReason.Transfer;
            //    modelMoney_Change_Log.MoneyBefore = modelStore.Money;
            //    modelMoney_Change_Log.MoneyAfter = modelMoney_Change_Log.MoneyBefore + modelMoney_Change_Log.Money;
            //    int LogID_new = bllMoney_Change_Log.Add(modelMoney_Change_Log);
            //    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Money_Change_Log, LogID_new, EnumHelper.OperateType.Add, "");
            //}
            ////确认订单
            //model.State = (int)EnumHelper.OrdersState.GetMoney;//修改订单状态:GetMoney
            //model.ExpectPriceSum = 0;//订单预计总计

            //foreach (Model.Orders_Goods modelOrders_Goods in bllOrders_Goods.GetModelList("OrdersID=" + Master.id))//货品：预计价格+预计小计+实际数量初始化
            //{
            //    Model.Goods modelGoods = bllGoods.GetModel(modelOrders_Goods.GoodsID);
            //    modelOrders_Goods.ExpectPrice = modelGoods.SellPrice;
            //    modelOrders_Goods.ExpectPriceSum = modelOrders_Goods.ExpectAmount * modelOrders_Goods.ExpectPrice;
            //    modelOrders_Goods.ActualAmount = modelOrders_Goods.ExpectAmount;
            //    bllOrders_Goods.Update(modelOrders_Goods);
            //    model.ExpectPriceSum += modelOrders_Goods.ExpectPriceSum;
            //}
            //bll.Update(model);
            //BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Orders, Master.id, EnumHelper.OperateType.Update, rbl_MoneyType.SelectedItem.Text + "收款，并确认预期数量和价格");
            ////订单状态日志
            //Model.Orders_State_Log modelOrders_State_Log = new Model.Orders_State_Log();
            //modelOrders_State_Log.OrdersID = Master.id;
            //modelOrders_State_Log.State = model.State;
            //int Orders_State_LogID_new = bllOrders_State_Log.Add(modelOrders_State_Log);
            //BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Orders_State_Log, Orders_State_LogID_new, EnumHelper.OperateType.Add, rbl_MoneyType.SelectedItem.Text + "收款，并确认预期数量和价格");

            Master.SubmitOK("发货成功");
        }

    
    }
}